package cn.myfirefly.sso.authentication.handler;

import javax.sql.DataSource;

import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.AuthenticationHandler;
import org.jasig.cas.authentication.handler.DefaultPasswordEncoder;
import org.jasig.cas.authentication.handler.NoOpPrincipalNameTransformer;
import org.jasig.cas.authentication.handler.PasswordEncoder;
import org.jasig.cas.authentication.handler.PrincipalNameTransformer;
import org.jasig.cas.authentication.principal.Credentials;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.myfirefly.sso.authentication.principal.CompanyCredentials;

public class CompanyAuthenticationHandler implements AuthenticationHandler {

	private static final Class<CompanyCredentials> DEFAULT_CLASS = CompanyCredentials.class;
	
	private PasswordEncoder passwordEncoder = (PasswordEncoder) new DefaultPasswordEncoder("MD5");
	
	private JdbcTemplate jdbcTemplate;
	
	private DataSource dataSource;
	
	private PrincipalNameTransformer principalNameTransformer = new NoOpPrincipalNameTransformer(); 
	
	/** Class that this instance will support. */
	private Class<?> classToSupport = DEFAULT_CLASS;
	private boolean supportSubClasses = true;
	
	public boolean authenticate(Credentials credentials)
			throws AuthenticationException {
		final CompanyCredentials xpc = (CompanyCredentials)credentials;
		final String username = getPrincipalNameTransformer().transform(xpc.getUsername());
		final String password = xpc.getPassword();
		final String encryptedPassword = this.getPasswordEncoder().encode(password);
		try {
			String sql = "select password from mm_user where login_name=? and status='1'";
			String dbPassword = getJdbcTemplate().queryForObject(sql, String.class, username);
			if (dbPassword != null && dbPassword.equals(encryptedPassword)) {
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		
		return false;
	}

	public boolean supports(Credentials credentials) {
		 return credentials != null  
	                && (this.classToSupport.equals(credentials.getClass()) || (this.classToSupport  
	                .isAssignableFrom(credentials.getClass()))  
	                && this.supportSubClasses); 
	}

	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource); 
		this.dataSource = dataSource;
	}

	public PrincipalNameTransformer getPrincipalNameTransformer() {
		return principalNameTransformer;
	}

	public void setPrincipalNameTransformer(
			PrincipalNameTransformer principalNameTransformer) {
		this.principalNameTransformer = principalNameTransformer;
	}

}
